package com.yanqu.road.server.channelserver.cmd;

import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.connect.ChannelCross3LinkedControl;
import com.yanqu.road.message.YanQuMessage;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public abstract class ChannelCommand implements Command {

    protected static Logger log = LogManager.getLogger(ChannelCommand.class.getName());
    private static Logger cmdLogger = LogManager.getLogger("COMMAND_TIME");
    protected long serverId;
    protected int ret;

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        serverId = ChannelCross3LinkedControl.getLinkedClient(ctx).getServerId();
        //	Date start = new Date();
        try {
            if (parseData(packet)) {
                execute(packet);
                sendData();
            }
        } catch (Exception e) {
            log.error("Cmd error, userId = " + packet.getPlayerId(), e);
        } finally {
		/*	Date endDate = new Date();
			long costTime = endDate.getTime() - start.getTime();
			if (costTime > 10) {
				cmdLogger.debug("exec cmd cost time {} Millis, userId {}, protocol Num:{} class:{}  ", costTime,
						packet.getPlayerId(), packet.getCode(), getClass().getSimpleName());
			}*/
        }
    }

    public abstract boolean parseData(YanQuMessage packet) throws Exception;

    public abstract void execute(YanQuMessage packet) throws Exception;

    public abstract void sendData();

}
